package com.ys.controller.api.mall;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ys.entity.mall.TmGiftUse;
import com.ys.service.flow.YspTaskService;
import com.ys.service.mall.TmGiftUseService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tmGiftUse")
public class ApiInterTmGiftUseController {
	
	public static Long MENU_ID = 102L;
	
	@Autowired
	private CommonApiService<TmGiftUse> commonApiService;
	@Autowired
	private YspTaskService yspTaskService;
	@Autowired
	private TmGiftUseService tmGiftUseService;

	@RequiresPermissions("tmGiftUse:view")
	@RequestMapping("/search")
	@ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TmGiftUse cond){
		cond.setOrderby("A.Id desc");
		
		cond.addLeftJoin("TS_UserInfo B on B.UserId = A.CreateUser")
			.addField("B.UserName userName");
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 流转通过
	 */
	@RequiresPermissions("tmGiftUse:flow")
	@PostMapping("/flowPass")
    @ResponseBody
	public R flowPass(HttpServletRequest request, Long id){
		try {
			return tmGiftUseService.flowPass(id, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(JSON.toJSONString(e));;
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 流转驳回
	 */
	@RequiresPermissions("tmGiftUse:flow")
	@PostMapping("/flowRefuse")
    @ResponseBody
	public R flowRefuse(HttpServletRequest request, Long id, String reason){
		
		TmGiftUse entity = new TmGiftUse().setId(id).queryFirst();
		String bid = entity.getBid();
		try {
			return yspTaskService.flowRefuse(bid, reason, ShiroUtils.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器正在开小差，请联系客服");
		}
	}
	
	/**
	 * 删除
	 * @throws Exception 
	 */
	@RequiresPermissions("tmGiftUse:flow")
	@PostMapping("/delete")
    @ResponseBody
	public R delete(Long id){
		
		try {
			return tmGiftUseService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("操作失败，请联系客服");
		}
	}
}